﻿using Microsoft.Win32;
using System.Diagnostics;
using System.Security.AccessControl;
using System.Security.Principal;

namespace 微信多开
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 获取微信安装路径
            string installPath = GetWeChatInstallPath();
            if (string.IsNullOrEmpty(installPath))
            {
                ShowLog("未找到微信安装路径！", "错误提示");
                return;
            }

            // 构建微信可执行文件完整路径
            string weChatExePath = System.IO.Path.Combine(installPath, "Weixin.exe");

            // 检查文件是否存在
            if (!System.IO.File.Exists(weChatExePath))
            {
                ShowLog("微信程序文件不存在！", "错误提示");
                return;
            }

            // 启用多开功能
            EnableMultiWeChat();

            // 启动微信
            try
            {
                Process.Start(new ProcessStartInfo(weChatExePath)
                {
                    UseShellExecute = true,
                    WindowStyle = ProcessWindowStyle.Normal
                });
            }
            catch (Exception ex)
            {
                ShowLog($"启动微信失败: {ex.Message}", "错误提示");
                return;
            }

        }

        /// <summary>
        /// 从注册表获取微信安装路径
        /// </summary>
        /// <returns>微信安装路径，获取失败返回null</returns>
        static string GetWeChatInstallPath()
        {
            try
            {
                // 打开注册表项
                using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Tencent\\Weixin"))
                {
                    if (key == null)
                    {
                        ShowLog("无法打开注册表键", "提示");
                        return null;
                    }

                    // 读取安装路径
                    string installPath = key.GetValue("InstallPath") as string;
                    if (string.IsNullOrEmpty(installPath))
                    {
                        ShowLog("无法读取注册表值", "提示");
                        return null;
                    }

                    return installPath;
                }
            }
            catch (Exception ex)
            {
                ShowLog($"获取安装路径失败: {ex.Message}", "错误");
                return null;
            }
        }
        /// <summary>
        /// 启用微信多开功能
        /// </summary>
        static void EnableMultiWeChat()
        {
            try
            {
                //4.0版本：_WeChat_App_Instance_Identity_Mutex_Name
                //4.1版本：XWeChat_App_Instance_Identity_Mutex_Name
                // 创建互斥体
                using (Mutex mutex = new Mutex(false, "XWeChat_App_Instance_Identity_Mutex_Name"))
                {
                    // 获取"Everyone"用户组的SID
                    SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

                    // 创建访问规则 - 拒绝所有访问

                    MutexAccessRule rule = new(
                        everyoneSid,
                        MutexRights.FullControl,
                        AccessControlType.Deny);

                    // 获取当前安全设置
                    MutexSecurity security = mutex.GetAccessControl();

                    // 添加访问规则
                    security.AddAccessRule(rule);

                    // 应用安全设置
                    mutex.SetAccessControl(security);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置多开权限失败: {ex.Message}", "错误");
            }
        }
        /// <summary>
        /// 显示消息框
        /// </summary>
        static void ShowLog(string message, string caption)
        {
            Console.WriteLine(caption + "：" + message);
            Console.ReadKey();
        }
    }


}
